/* Regression analysis for the alternative market definition: HRR   */
clear all
set more off
cd ""   //target directory



*** Construct `ahaid_hrr'
*!!! Need to re-define MARKET to be HRR !!!
clear
insheet using "./prepare/hinfo_hrr.csv",clear
rename (v1 v2 v3 v4 v5 v6) (ahaid year prechs adj_vnd maxhrr sysid)
keep ahaid year maxhrr
tempfile othermkt
save `othermkt'


insheet using choice.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newchoice`i'
}
reshape long newchoice,i(ahaid year) j(vid)
tempfile chs
save `chs'

insheet using vlag.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvlag`i'
}
reshape long newvlag,i(ahaid year) j(vid)
tempfile vlag
save `vlag'


insheet using exov1mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1mkt`i'
}
reshape long newv1mkt,i(ahaid year) j(vid)
tempfile v1mkt
save `v1mkt'


insheet using exov2mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2mkt`i'
}
reshape long newv2mkt,i(ahaid year) j(vid)
tempfile v2mkt
save `v2mkt'


insheet using exovmksh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvmktsh`i'
}
reshape long newvmktsh,i(ahaid year) j(vid)
tempfile vmktsh
save `vmktsh'


insheet using v1sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1sys`i'
}
reshape long newv1sys,i(ahaid year) j(vid)
tempfile v1sys
save `v1sys'


insheet using v2sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2sys`i'
}
reshape long newv2sys,i(ahaid year) j(vid)
tempfile v2sys
save `v2sys'


insheet using vsysh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvsysh`i'
}
reshape long newvsysh,i(ahaid year) j(vid)
tempfile vsysh
save `vsysh'


insheet using exovtipmsh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvtipmsh`i'
}
reshape long newvtipmsh,i(ahaid year) j(vid)
merge 1:1 ahaid year vid using `chs'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vlag'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vmktsh'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vsysh'
keep if _merge==3
drop _merge
tempfile vtipmsh
save `vtipmsh'



//IV inputs  ### UPDATED on 07/31/2019
insheet using "./prepare/iv_dom_tmp.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivdom`i'
}
reshape long ivdom,i(hsayr_id) j(vid)
tempfile ivdom
save `ivdom'


insheet using "./prepare/iv_msh_tmp.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivmsh`i'
}
reshape long ivmsh,i(hsayr_id) j(vid)
tempfile ivmsh
save `ivmsh'




clear
use "./prepare/allbasic.dta",clear
***!!! RENAME hsa to hrr!!!
merge m:1 ahaid year using `othermkt'
keep if _merge==3
drop _merge
rename hsa hsa_ori
rename maxhrr hsa


sort sysid year
egen sysyr_id=group(sysid year)
sort hsa year
egen hsayr_id=group(hsa year)


merge 1:1 ahaid year vid using `vtipmsh'
keep if _merge==3
drop _merge


merge m:1 hsayr_id vid using `ivmsh'
drop _merge


merge m:1 hsayr_id vid using `ivdom'
drop _merge




****************** Now incorporate new inputs for market (un)observables
***%%%*** Demographics: population and income per capita
preserve
clear
use "./prepare/demographic4hrr"
rename maxhrr hsa
duplicates drop hsa year,force
keep hsa year totpop totpop65 avginc
gen perc65=totpop65/totpop
replace totpop=log(totpop)
replace totpop65=log(totpop65)
replace avginc=log(avginc)
tempfile pop
save `pop'
restore
***%%%***



***%%%*** Create HHI
preserve
keep if vid==13
keep id year admtot hsa
bys hsa year:egen totadm_mkt=total(admtot)
gen admsh=admtot/totadm_mkt
bys hsa year:egen hhi=total(admsh^2)
duplicates drop hsa year,force
keep hsa year hhi
tempfile hhi
save `hhi'
restore
***%%%***



***%%%*** ### UPDATED on 04/15/2020: Use the combined market FEs---mktFEs_comb1 (using adoption ratio as DV)
preserve

clear
input hsa mktfe
1 0
end
tempfile firsthsa
save `firsthsa'


clear
import delim using "./prepare/mktFEs_comb1.csv",delimiters(",") varnames(1)
rename (v1 comb) (hsa mktfe)
drop if strpos(hsa,"_Ihsa_")==0
replace hsa=subinstr(hsa,"_Ihsa_","",1)
destring,replace force
append using `firsthsa',force

gen below25cat=0
gen below50cat=0
gen below75cat=0
gen below100cat=0

sum mktfe,detail

replace below25cat=1 if mktfe<=r(p25)
replace below50cat=1 if mktfe<=r(p50)&mktfe>r(p25)
replace below75cat=1 if mktfe<=r(p75)&mktfe>r(p50)
replace below100cat=1 if mktfe>r(p75)

tempfile mktfe_comb
save `mktfe_comb'

restore


***%%%***



** MErge stuff

merge m:1 hsa using `mktfe_comb'
keep if _merge==3
drop _merge


merge m:1 hsa year using `pop'
keep if _merge==3
drop _merge


merge m:1 hsa year using `hhi'
keep if _merge==3
drop _merge

**




sum bdtot if sysid==0,detail  //bdtot: 25%: 25; 50%: 80; 75%: 193
gen bdrng=0
replace bdrng=1 if bdtot<=r(p25)
replace bdrng=2 if bdtot>r(p25) & bdtot<=r(p50)
replace bdrng=3 if bdtot>r(p50) & bdtot<=r(p75)
replace bdrng=4 if bdtot>r(p75)

tab bdrng,gen(bdqntl)

foreach i in 1 2 3 4 {
gen v1bd`i'=newv1mkt*bdqntl`i'
gen vmshbd`i'=newvmktsh*bdqntl`i'
gen iv1bd`i'=ivdom*bdqntl`i'
gen ivmshbd`i'=ivmsh*bdqntl`i'
}



gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1


forvalues i=2/13 {
gen bed`i'=bdtot*(vid==`i')
gen mkthhi`i'=hhi*(vid==`i')
gen mktpop65`i'=totpop65*(vid==`i')
}




*** NON-IV estimation
xi:clogit newchoice vmshbd* i.vid bed* below50cat below75cat below100cat mkthhi* mktpop65* if sysid==0&prechs==1,group(newid) vce(r)  
est sto noiv



*** IV estimation
reg newvmktsh ivdom ivmsh if sysid==0&prechs==1
predict uhat,residual
gen uhat2=uhat*uhat


*** CF
xi:clogit newchoice vmshbd* i.vid bed* below50cat below75cat below100cat mkthhi* mktpop65* if sysid==0&prechs==1&ivmsh~=.,group(newid) vce(r) 
est sto noiv_iv


xi:clogit newchoice vmshbd* uhat uhat2 i.vid bed* below50cat below75cat below100cat mkthhi* mktpop65* if sysid==0&prechs==1,group(newid) vce(boot,rep(50))  
est sto cf


** LPM
xi:ivreg2 newchoice vmshbd* i.vid bed* below50cat below75cat below100cat mkthhi* mktpop65* if sysid==0&prechs==1,robust 
est sto noiv2sls


*** LPM on IV sample
xi:ivreg2 newchoice i.vid vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* below50cat below75cat below100cat mkthhi* mktpop65* if sysid==0&prechs==1&ivmsh~=.,robust  
est sto noiv2sls_iv


*** 2SLS
bootstrap, reps(50):ivreg2 newchoice _Ivid_* bed* below50cat below75cat below100cat mkthhi* mktpop65* (vmshbd1 vmshbd2 vmshbd3 vmshbd4=iv1bd1 iv1bd2 iv1bd3 iv1bd4 ivmshbd1 ivmshbd2 ivmshbd3 ivmshbd4) if sysid==0&prechs==1,robust  
est sto iv




